package num12;

public class Leecode27 {
    public  class ListNode{
        int val;
        ListNode next;
        ListNode(){

        }
        ListNode(int val){
            this.val = val;
        }
        ListNode(int val,ListNode next){
            this.val = val;
            this.next = next;
        }
    }
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode listNode = new ListNode(-1);
        //创建一个prev虚拟节点
        ListNode prev = listNode;
        while (list1!=null && list2!=null){
            if (list1.val >= list2.val){
                prev.next = list2;
                list2= list2.next;
            }else {
                prev.next = list1;
                list1 = list1.next;
            }
            prev = prev.next;
        }

        prev.next = (list1 == null) ? list2 : list1;
        return prev;



    }
}
